package com.ideaction.game.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import com.ideaction.game.annotation.Rule;
import com.ideaction.game.bus.OrderType;
import com.ideaction.game.bus.RedissionBusService;
import com.ideaction.game.rule.IRule;
import com.ideaction.game.rule.RuleManager;
import com.ideaction.game.xmldata.ISample;
import com.ideaction.game.xmldata.SampleContainer;
//BeanDefinitionRegistryPostProcessor了解
@Component
public class RegistBeanPostProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		//注册条件判断规则bean
		if (bean instanceof IRule) {
			IRule irule = (IRule) bean;
			Rule annotation = irule.getClass().getAnnotation(Rule.class);
			if (annotation == null) {
				try {
					throw new Exception("IRule 实现类必须关联 Rule注解中的功能id");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			RuleManager.regist(annotation.ruleFuncId(), irule);
		}
		//注册策划配置bean
		if (bean instanceof ISample) {
			ISample isample = (ISample) bean;
			SampleContainer.registDefinedClass(isample.getClass().getSimpleName(), isample.getClass());
		}
		// 注册BI命令
		if (bean instanceof OrderType) {
			OrderType orderType = (OrderType) bean;
			RedissionBusService.registOrderTypeMap(orderType.getOrderId(), orderType);
		}
		return null;
	}
}
